﻿Namespace B1Data
    ''' <summary>
    ''' 不能继承DataRow顾重载了强转操作
    ''' B1Datarow
    ''' </summary>
    ''' <remarks></remarks>
    Public Class B1DataRow
        'Inherits Data.DataRow
        Implements B1Data.IB1DataRow

        Private dataRow As Data.DataRow
        Private _sql_insert As String = "INSERT INTO dbo.[%0] (%1) Values (%3)"

        'Public Sub New(ByVal builder As DataRowBuilder)
        '    MyBase.New(builder)
        'End Sub

        Public ReadOnly Property B1Table As B1DataTable Implements IB1DataRow.B1Table
            Get
                Return Me.dataRow.Table
            End Get
        End Property

        ''' <summary>
        ''' 已重载的强转操作符
        ''' </summary>
        ''' <param name="dataRow"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Widening Operator CType(ByVal dataRow As DataRow) As B1DataRow
            Dim item As New B1DataRow()
            item.dataRow = dataRow
            Return item
        End Operator

        ''' <summary>
        ''' 已重载的强转操作符
        ''' </summary>
        ''' <param name="item"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Widening Operator CType(ByVal item As B1DataRow) As DataRow
            Return item.dataRow
        End Operator

        ''' <summary>
        ''' 根据行状态获取SQL语句
        ''' 添加——Insert
        ''' 更新——Delete and Insert
        ''' 删除——Delete
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function getRowSQL() As String Implements IB1DataRow.getRowSQL
            Dim sql_return As String = ""
            Try
                If dataRow.RowState = DataRowState.Added Then
                    sql_return = _sql_insert.Remove("%0", Me.B1Table.TableName).Remove("%1", Me.B1Table.ColumnsString).Remove("%3", Me.getInasertSQLValues)

                End If
                If dataRow.RowState = DataRowState.Deleted Then

                End If
                If dataRow.RowState = DataRowState.Modified Then

                    sql_return = _sql_insert.Remove("%0", Me.B1Table.TableName).Remove("%1", Me.B1Table.ColumnsString).Remove("%3", Me.getInasertSQLValues)
                End If
                Return Nothing
            Catch ex As Exception
                Throw
            End Try
        End Function

        ''' <summary>
        ''' 获取SQL值
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function getInasertSQLValues() As String
            Dim col As Data.DataColumn
            Dim values As String = ""
            For Each col In Me.B1Table.Columns
                values += Me.getFieldValueSQL(col, dataRow.Item(col)) & ","
            Next
            If values <> "" Then
                values = values.Remove(values.Length - 1, 1)
            End If
            Return values
        End Function

        Private Function getFieldValueSQL(ByVal col As DataColumn, ByVal value As Object) As String
            Select Case col.DataType.GetType
                Case GetType(String)
                    Return "'" & value & "'"
                Case GetType(Int32)
                    Return value
                Case GetType(Decimal)
                    Return value
                Case GetType(DateTime)
                    Return "'" & value & "'"
                Case Else
                    Return "'" & value & "'"
            End Select
        End Function

        ''' <summary>
        ''' cell属性
        ''' </summary>
        ''' <param name="columnIndex"></param>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Default Public Property Item(ByVal columnIndex As Integer) As Object Implements IB1DataRow.Item
            Get
                Dim Col As B1Data.B1DataColumn
                Col = Me.B1Table.Columns.Item(columnIndex)
                Return Me.Item(Col)
            End Get
            Set(ByVal value As Object)
                Dim Col As B1Data.B1DataColumn
                Col = Me.B1Table.Columns.Item(columnIndex)
                Me.Item(Col) = value
                'dataRow.Item(columnIndex) = FormatCellValue(value)
            End Set
        End Property

        ''' <summary>
        ''' cell属性
        ''' </summary>
        ''' <param name="columnName"></param>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Default Public Property Item(ByVal columnName As String) As Object Implements IB1DataRow.Item
            Get
                Dim Col As B1Data.B1DataColumn
                Col = Me.B1Table.Columns.Item(columnName)
                Return Me.Item(Col)
            End Get
            Set(ByVal value As Object)
                Dim Col As B1Data.B1DataColumn
                Col = Me.B1Table.Columns.Item(columnName)
                Me.Item(Col) = value
            End Set
        End Property

        ''' <summary>
        ''' cell属性
        ''' </summary>
        ''' <param name="column"></param>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Default Public Property Item(ByVal column As B1Data.B1DataColumn) As Object Implements IB1DataRow.Item
            Get
                Return dataRow.Item(column)
            End Get
            Set(ByVal value As Object)
                Dim oldValue As Object
                oldValue = dataRow.Item(column)
                Try
                    '检测赋值数据是否合法
                    If column.CellCheck Is Nothing Then
                    Else
                        If column.CellCheck.Check(Me, value) = False Then
                            Throw New Exception(column.CellCheck.Error)
                        End If
                    End If

                    dataRow.Item(column) = FormatCellValue(value)
                    '字段改变后执行后续业务逻辑处理
                    If column.CellLogic Is Nothing Then
                    Else
                        dataRow.BeginEdit()
                        If column.CellLogic.DoLogic(Me) = False Then
                            dataRow.CancelEdit()
                            If column.CellLogic.Rollback Then
                                dataRow.Item(column) = oldValue
                            End If
                            Throw New Exception(column.CellLogic.Error)
                        End If
                        dataRow.EndEdit()
                    End If
                    '字段依赖关系
                    If column.Dependents Is Nothing Then
                    Else
                        column.Dependents.GO(Me)
                    End If
                Catch ex As Exception
                    Throw ex
                End Try
            End Set
        End Property

        ''' <summary>
        ''' 格式化值到cell
        ''' </summary>
        ''' <param name="value"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function FormatCellValue(ByVal value As Object) As Object
            If value Is Nothing Then
                Return DBNull.Value
            Else
                Return value
            End If
        End Function

    End Class
End Namespace
